import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import Login from '../views/Login.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    redirect: '/home/user',
    children: [
      { path: 'user', component: () => import('../views/UserView.vue')},
      { path: 'admin', component: () => import('../views/AdminView.vue') },
      {
        path: 'pets', component: () => import('../views/PetsView.vue'), redirect: '/home/pets/dog',
        children: [{ path: 'dog', component: () => import('../views/PetsDog.vue') }, { path: 'cat', component: () => import('../views/PetsCat.vue') }]
      },
    ]
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//挂载路由导航守卫,to表示将要访问的路径，from表示从哪里来，next是下一个要做的操作
router.beforeEach((to,from,next)=>{
  if(to.path === '/login')
  return next();
  //获取token
  const tokenStr = window.sessionStorage.getItem('code');
  if(tokenStr!=200)
  return next('/login');
  next();
  })
  

export default router
